﻿using HHG.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace OnLineExam.Web.handler
{
    /// <summary>
    /// userAnswerHandler 的摘要说明
    /// </summary>
    public class userAnswerHandler : IHttpHandler
    {

        private HttpContext _httpContext;
        private HttpRequest _httpRequest;
        private HttpResponse _httpResponse;
        private Data.OnlineExamEntities _ctx;
        private OnLineExam.Data.EfRepository<OnLineExam.Data.Score> _scoreRepository;
        private OnLineExam.Data.EfRepository<OnLineExam.Data.UserAnswer> _userAnswerRepository;
        public void ProcessRequest(HttpContext context)
        {
            _ctx = new Data.OnlineExamEntities();
            _httpContext = context;
            _httpRequest = context.Request;
            _httpResponse = context.Response;
            _scoreRepository = new Data.EfRepository<Data.Score>(_ctx);
            _userAnswerRepository = new Data.EfRepository<Data.UserAnswer>(_ctx);

            context.Response.ContentType = "text/plain";
            var type = context.Request.Params["type"];

            switch (type)
            {
                case "getlist":
                    GetList();
                    break;

                default:
                    break;
            }
        }
        private void GetList()
        {

            var id = _httpRequest.Params["id"].GetInt();
            var qType = _httpRequest.Params["qtype"];//试题类型

            var score = _scoreRepository.GetById(id);
            var answers = _userAnswerRepository.TableNoTracking.Where(s => s.ScoreID == id);

            using (var ctx = _ctx)
            {
                var spList = ctx.SingleProblem.Where(s => s.PaperID == score.PaperID).ToList();
                var mpList = ctx.MultiProblem.Where(s => s.PaperID == score.PaperID).ToList();
                var jpList = ctx.JudgeProblem.Where(s => s.PaperID == score.PaperID).ToList();
                var fpList = ctx.FillBlankProblem.Where(s => s.PaperID == score.PaperID).ToList();
                var qpList = ctx.QuestionProblem.Where(s => s.PaperID == score.PaperID).ToList();


                var query = from ua in ctx.UserAnswer
                            where ua.ScoreID == id && ua.Type != "开始考试"
                            select ua;
                if (qType.HasValue())
                    query = query.Where(s => s.Type == qType);


                var result = query.Select(ua => new UsreAnswerView
                {
                    ID = ua.ID,
                    ScoreID = ua.ScoreID,
                    PaperName = ua.Paper.PaperName,
                    UserID = ua.UserID,
                    UserMark = ua.UserMark,
                    Mark = ua.Mark,
                    UserAnswer1 = ua.UserAnswer1,
                    TitleID = ua.TitleID,
                    QuestionTypeStr = ua.Type,
                    ExamTime = ua.ExamTime,

                }).OrderBy(s => s.QuestionTypeStr).ToList();

                var replace = new List<string> { "A:A", "A:A" };
                result.ForEach(m =>
                {
                    m.QuestionType = Data.Paper.GetQuestionTypeEnum(m.QuestionTypeStr);
                    switch (m.QuestionType)
                    {
                        case Data.QuestionType.Single:
                            var sTemp = spList.First(s => s.ID == m.TitleID);
                            m.QuestionTypeStr = "单选题";
                            m.QuestionAnswer = sTemp.Answer;
                            m.Title = sTemp.Title;
                            //m.Question = string.Format("A:{0},B:{1},C:{2},D:{3}",sTemp.AnswerA,sTemp.AnswerB,sTemp.AnswerC,sTemp.AnswerD);
                            m.Question = sTemp;
                            // m.Question = m.Question.Replace("A:A","").Replace("B:B", "").Replace("B:B", "");
                            break;
                        case Data.QuestionType.Multi:
                            var mTemp = mpList.First(s => s.ID == m.TitleID);
                            m.QuestionAnswer = mTemp.Answer;
                            m.Title = mTemp.Title;
                            m.QuestionTypeStr = "多选题";
                            m.Question = mTemp;
                            //m.Question = string.Format("A:{0},B:{1},C:{2},D:{3},E:{4},F:{5},G:{6}", mTemp.AnswerA, mTemp.AnswerB, mTemp.AnswerC, mTemp.AnswerD, mTemp.AnswerE, mTemp.AnswerF, mTemp.AnswerG);
                            break;
                        case Data.QuestionType.Judge:
                            var jTemp = jpList.First(s => s.ID == m.TitleID); ;
                            m.QuestionAnswer = jTemp.Answer ? "正确" : "错误";
                            m.Title = jTemp.Title;

                            m.QuestionTypeStr = "判断题";
                            m.Question = jTemp;
                            break;
                        case Data.QuestionType.Fill:
                            var fTemp = fpList.First(s => s.ID == m.TitleID);
                            m.QuestionAnswer = fTemp.Answer;
                            m.Title = fTemp.Title;
                            m.QuestionTypeStr = "填空题";
                            m.Question = fTemp;
                            break;
                        case Data.QuestionType.Qustion:
                            var qTemp = qpList.First(s => s.ID == m.TitleID);

                            m.QuestionAnswer = qTemp.Answer;
                            m.Title = qTemp.Title;
                            m.QuestionTypeStr = "问答题";
                            m.Question = qTemp;
                            break;
                        case Data.QuestionType.Other:
                            break;
                        default:
                            break;
                    }
                });


                WriteJson(EasyuiDataGrid.Create(result.Count(), result.OrderBy(s=>s.ID).OrderBy(s=>s.QuestionType) .ToList()));
            }




        }


        private void WriteJson(object data)
        {
            _httpResponse.Write(data.SerializeObject());
            _httpResponse.End();
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
    public class UsreAnswerView
    {
        public int ID { get; set; }
        public int ScoreID { get; set; }
        public string PaperName { get; set; }
        public string UserID { get; set; }
        public int UserMark { get; set; }
        public int Mark { get; set; }
        public string UserAnswer1 { get; set; }
        public int TitleID { get; set; }
        public string Title { get; set; }
        public Data.QuestionType QuestionType { get; set; }
        public string QuestionTypeStr { get; set; }
        public DateTime ExamTime { get; set; }
        /// <summary>
        /// 答案
        /// </summary>
        public string QuestionAnswer { get; set; }
        public object Question { get; set; }

    }
}